//
//  indexVC.m
//  first
//
//  Created by 毕小强 on 16/12/9.
//  Copyright © 2016年 毕小强. All rights reserved.
//

#import "indexVC.h"

@interface indexVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong) UITableView *mainTableView;
@property(nonatomic,strong)NSArray *dataArr;
@property(nonatomic,strong)NSMutableDictionary *mSelectionDic;
@property(nonatomic,strong)NSMutableArray *mIndexArr;
@end

@implementation indexVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.mSelectionDic = [NSMutableDictionary dictionary];
    self.mIndexArr = [NSMutableArray array];
    self.dataArr = @[@"太平人寿",@"太平洋寿险",@"中国人保",@"安邦人寿",@"安盛天下",@"北大方正",@"中国人寿",@"中国平安",@"新华保险",@"泰康保险",@"友邦保险",@"中国太平",@"阳光保险",@"生命人寿",@"人保股份",@"腾保保险",@"阳光人寿",@"安邦人寿",@"人保寿险"];
    [self ProcessData];
    self.mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    [self.view addSubview:_mainTableView];
}
#pragma mark UITableViewDelegate,UITableViewDataSource
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    NSMutableArray *mArr = [_mSelectionDic objectForKey:_mIndexArr[indexPath.section]];
    cell.textLabel.text = mArr[indexPath.row];
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSMutableArray *mArr = [_mSelectionDic objectForKey:_mIndexArr[section]];
    return mArr.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _mIndexArr.count;
}
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return  _mIndexArr[section];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}
-(nullable NSArray<NSString*>*)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return _mIndexArr;
}
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
    return index;
}
#pragma mark Data
-(void)ProcessData{
    for (int i = 0; i<_dataArr.count; i++) {
        NSString *Str = _dataArr[i];
        if (Str.length) {
            NSMutableString *ms = [[NSMutableString alloc]initWithString:Str];
            //汉字转拼音
            if (CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformMandarinLatin, NO)) {
                //拼音转英文
                if (CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformStripDiacritics, NO)) {
                    NSString *firstStr = [ms substringToIndex:1];
                    if (![self MatchLetter:firstStr]) firstStr = @"#";
                    if (_mIndexArr.count == 0) {
                        [_mIndexArr addObject:firstStr];
                        NSMutableArray *mArr =[NSMutableArray arrayWithObject:Str];
                        [self.mSelectionDic setObject:mArr forKey:firstStr];
                    }else{
                        if ([_mIndexArr containsObject:firstStr]) {
                        //包含了索引字母
                            NSMutableArray *mArr = [_mSelectionDic objectForKey:firstStr];
                            [mArr addObject:Str];
                            [self.mSelectionDic setObject:mArr forKey:firstStr];
                        }else{
                        //没有包含索引字母
                            [_mIndexArr addObject:firstStr];
                            NSMutableArray *mArr = [NSMutableArray arrayWithObject:Str];
                            [self.mSelectionDic setObject:mArr forKey:firstStr];
                        }
                    }
                }
            }
        }
    }
    NSLog(@"排序前:%@",self.mIndexArr);
    self.mIndexArr = [NSMutableArray arrayWithArray:[self.mIndexArr sortedArrayUsingSelector:@selector(compare:)]];
    NSLog(@"排序后:%@",self.mIndexArr);
}
#pragma mark - 匹配是不是字母开头
-(BOOL)MatchLetter:(NSString *)str
{
    //判断是否以字母开头
    NSString *ZIMU = @"^[A-Za-z]+$";
    NSPredicate *regextestA = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", ZIMU];
    
    if ([regextestA evaluateWithObject:str] == YES)
        return YES;
    else
        return NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
